<template>
  <DataList 
  :cate="cate" 
  :cols="cols"
  :name="name"
  :excelHeader="excelHeader"
  :groupCheck = "groupCheck" 
 ref="datalist"
  skipExcelNumber="2" />
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import {get开设专业} from  "@/api/开设专业"
import { get教师基本情况ByGH, get教师基本情况 } from "@/api/教师基本情况";
import { addZyfzr, deleteZyfzr, updateZyfzr, getZyfzr, bulkAddZyfzr } from "@/api/专业负责人.js";
import {findComponent} from "@/utils/ui";
import linq from "linq"
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: "A7-1-3专业负责人表",
      name: "7.1.3 专业负责人",
      excelHeader:{B3:"专业所属系部代码",C3:"专业所属系部名称",D3:"专业代码",E3:"专业名称（全称）",F3:"教工号",G3:"姓名",H3:"区号－单位电话号码",I3:"电子邮箱",J3:"担任专业负责人工作年限（年）"},

      cols: [
        { label: "专业所属系部代码", prop: "BMBH" },
        { label: "专业所属系部名称", prop: "BMMC" },
        { label: "专业代码", prop: "ZYDM" },
        { label: "专业名称(全称)", prop: "ZYMC" },
        { label: "教工号", prop: "GH" },
        { label: "姓名", prop: "XM" },
        { label: "教师性质", prop: "XZ_JSXZ" },
        { label: "性别", prop: "XZ_XB" },
        { label: "出生日期", prop: "CSRQ" },
        { label: "学历", prop: "XZ_XL" },
        { label: "学位", prop: "XZ_XW" },
        { label: "工作单位名称(全称)", prop: "DWMC" },
        { label: "职务", prop: "ZW" },
        { label: "区号－单位电话号码", prop: "DHHM" },
        { label: "电子邮箱", prop: "DZYX" },
        { label: "担任专业带头人工作年限(年)", prop: "DRZYFZRGZNX" },
        
        { label: "专业技术职务(最高)" ,data:[
            {label:"等级",prop:"XZ_ZYJSZW_DJ"},{ label: "名称(全称)", prop: "ZYJSZW_MC" },
        { label: "发证单位", prop: "ZYJSZW_FZDW" },{ label: "获取日期(年月)", prop: "ZYJSZW_HQRQ" }
        ]}
      ],
      groupCheck:[{data:["XZ_ZYJSZW_DJ","ZYJSZW_MC","ZYJSZW_FZDW","ZYJSZW_HQRQ"],desc:'请完善专业技术职务的输入'}]
    };
  },
  methods: {
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
         sums[0]="合计";
         sums[6]="系部数:"+linq.from(list).distinct("$.BMBH").count()
         sums[8]="专业数:"+linq.from(list).distinct("$.ZYDM").count()
         sums[10]="教师数:"+linq.from(list).distinct("$.GH").count()
         sums[13]= "男:"+linq.from(list).where("$.XZ_XB=='男'").count();
         sums[13] += ",女:"+linq.from(list).where("$.XZ_XB=='女'").count();
         sums[15]="大学或以上:"+linq.from(list).where("$.XZ_XL!=='专科'&&$.XZ_XL!=='专科以下'").count()
         sums[16]="有学位:"+linq.from(list).where("$.XZ_XW!==''").count()
         sums[21]="平均:"+linq.from(list).average("$.DRZYFZRGZNX").toFixed(0)
         sums[22]="中级或以上:"+linq.from(list).where("$.XZ_ZYJSZW_DJ=='高级'||$.XZ_ZYJSZW_DJ=='中级'").count()
         sums[23]="具有专业技术职务:"+linq.from(list).where("$.ZYJSZW_MC").count()
         sums[31]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
         sums[31]+= ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
         sums[31] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
 
       return sums;
      //
    },
    onDlgCreated(model) {
       
      // var dd = findComponent(this,"BMMC");
      // var 专业 = linq.from(this.$root.开设专业).distinct("$.BMMC").toArray();
      // dd.$attrs.items = 专业.map(item => {
      //   return { label: item.BMMC, value: item.BMMC};
      // });
      
    },
    async  bulkAdd(array){
       await  bulkAddZyfzr({Zyfzr:array});
    },
    //别的页面不用复制此方法
    
    /*onChange(model, field, value) {
      //
      
      if (field == "BMMC") {
        var zydm =  linq.from(this.$root.开设专业).where(item=>item.BMMC==value).distinct("$.ZYDM").toArray();
        var dd = findComponent(this,"ZYMC");
        dd.$attrs.items =zydm.map(item => {
          return { value: item.ZYMC, label: item.ZYMC,zydm:item.ZYDM };
        });
        
        model.ZYMC = dd.$attrs.items[0].value;
        model.ZYDM = dd.$attrs.items[0].zydm;
        //dd.$children[0].setCurrentValue(dd.$attrs.items[0].ZYDM);
      }
      if(field == "ZYMC"){
        
        var zydm =  linq.from(this.$root.开设专业).where(item=>item.ZYMC==value).toArray();
        //var dd = findComponent(this,"ZYDM");
        model.ZYDM = zydm[0].ZYDM;
      }
      //
    },*/
    async onChange(model, field, value) {
      
       if(field == "GH"){
         if(value == "error"){
           console.log("error")
         
          model.GH="";
          model.BMBH="";
          model.BMMC="";
          model.ZYDM=""
          model.ZYMC=""
          model.XM= "";
          model.XZ_JSXZ="";
          model.XZ_XB="";
          model.CSRQ="";
          model.XZ_XL="";
          model.XZ_XW="";
          model.DWMC="";
          model.ZW = "";
        
         }else{
         var d1 =  await get教师基本情况ByGH(this.$root.user.XXID,this.$root.user.CJPC,value);
        
           console.log(d1[0])
        
          model.XM= d1[0].XM;
          model.BMBH=d1[0].BMBH;
          model.BMMC=d1[0].BMMC;
          model.ZYDM=d1[0].ZYSKZYDM;
          model.ZYMC=d1[0].ZYSKZYMC;
          model.XZ_JSXZ=d1[0].教师性质;
          model.XZ_XB=d1[0].XZ_XB;
          model.CSRQ=d1[0].CSRQ;
          model.XZ_XL=d1[0].XZ_XL;
          model.XZ_XW=d1[0].XZ_XW;
          model.DWMC=d1[0].DWMC;
          model.ZW = d1[0].ZYJSZW_MC;
       
         }
       }
      //
    },
   check(array,list) {
     return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          //
          var 专业 = await  get开设专业(this.$root.user.XXID,this.$root.user.CJPC);
          var err =  linq.from(array).select("$.ZYDM").except(linq.from(专业).select("$.ZYDM").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.ZYDM) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中不存在专业代码");
         
          var 教工号 = await get教师基本情况(user.XXID,user.CJPC)
           
        //是否已经存在相同
          
          var err =  linq.from(array).select("$.GH").except(linq.from(教工号).select("$.GH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.GH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中教师基本情况表不存在对应的教工号");
          //把错误的教师姓名替代掉
          err =  linq.from(array).select("$.GH").intersect(linq.from(教工号).select("$.GH").toArray()).toArray();
          
          linq.from(array).where(e=>err.indexOf(e.GH) !==-1).
          forEach(e=>{
            var d = linq.from(教工号).firstOrDefault(item=>item.GH == e.GH);
         
            if(d){
              
              e.XM = d.XM;
              e.BMMC = d.BMMC;
              e.BMBH = d.BMBH;
              e.XZ_JSXZ=d.教师性质;
              e.XZ_XB=d.XZ_XB;
              e.CSRQ=d.CSRQ;
              e.XZ_XL=d.XZ_XL;
              e.XZ_XW=d.XZ_XW;
              e.DWMC=d.DWMC;
              e.ZW = d.ZYJSZW_MC;
            
            }
          });
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },
    async fetchData() {
      var user =  getUser()
      var datas = await getZyfzr(user.XXID,user.CJPC);
      return datas;
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
      
          console.log(JSON.stringify(temp));
          var model = await addZyfzr(temp);
          resolve(model);
      })
      
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateZyfzr(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
         await deleteZyfzr(temp);
          resolve();
      })
    }
  }
};
</script>
<style>
</style>